package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.exception.AuthenticationFailedException;
import com.itheima.common.exception.DataNotFoundException;
import com.itheima.common.exception.DuplicateDataException;
import com.itheima.common.exception.NoSpaceException;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
import java.util.Objects;

@Service
public class OrderServiceimpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Override
    public OrderVO findById(Integer id) {
        Order order=orderMapper.findById(id);
        if(order==null){
            throw new DataNotFoundException(MessageConstant.QUERY_ORDER_FAIL);
        }
        Member member = memberMapper.selectById(order.getMemberId());
        Setmeal setmeal=setmealMapper.selectById(order.getSetmealId());
        return new OrderVO(order.getOrderType(),member.getName(), order.getOrderDate() , setmeal.getName());
    }

    @Override
    public Integer addOrder(OrderDTO orderDTO) {

        //验证码部分
        String phone=orderDTO.getTelephone();
        String validCode=(String) redisTemplate.opsForValue().get(phone + "Order");
        if (!Objects.equals(validCode, orderDTO.getValidateCode()) && !"0000".equals(orderDTO.getValidateCode())) {
            throw new AuthenticationFailedException(MessageConstant.VALIDATECODE_ERROR);
        }
        redisTemplate.delete(phone + "Order");
        //查询是否还有预约空间
        OrderSetting  orderSetting=orderSettingMapper.selectByDate(orderDTO.getOrderDate());
        if (orderSetting == null || orderSetting.getNumber() <= orderSetting.getReservations()) {
            throw new NoSpaceException(MessageConstant.ORDER_FULL);
        }
        Member member=memberMapper.selectByIDCard(orderDTO.getIdCard());
        if (member == null) {
            member = new Member();
            member.setName(orderDTO.getName());
            member.setSex(orderDTO.getSex());
            member.setIdCard(orderDTO.getIdCard());
            member.setPhoneNumber(orderDTO.getTelephone());
            member.setRegTime(Date.from(Instant.now()));
            memberMapper.addMember(member);
        }
        Integer count = orderMapper.countByMemberIdAndDateWithSetmeal(member.getId(), orderDTO.getOrderDate(), orderDTO.getSetmealId());
        if (count > 0) {
            // 今日已经有了预约,预约失败
            throw new DuplicateDataException(MessageConstant.ORDERSETTING_FAIL);
        }
        Order order = new Order();
        BeanUtils.copyProperties(orderDTO, order);
        order.setMemberId(member.getId());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderMapper.insert(order);
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingMapper.update(orderSetting);
        return order.getId();
    }
}
